<template>
  <!-- 新增投诉 -->
  <view class="newComplaint">
    <view class="wrapper">
      <view class="wrapper-item content">
        <u-form :model="form" ref="form" label-width="100" labelPosition="’top‘" :error-type="errorType">
          <u-form-item label="选择派出所" :borderBottom="false" :required="true" @click="policeAgencyFoces">
            <u--input v-model="form.policeAgencyName" disabled disabledColor="#ffffff" placeholder="请选择线索发生地所属派出所"
              border="none"></u--input>
            <u-icon slot="right" name="arrow-right"></u-icon>
          </u-form-item>

          <u-form-item label="选择投诉分类" :borderBottom="false" :required="true"
            @click="cellClick('text', 'complaintClassificationDesc')">
            <u--input v-model="form.complaintClassificationDesc" disabled disabledColor="#ffffff" placeholder="请选择投诉分类"
              border="none"></u--input>
            <u-icon slot="right" name="arrow-right"></u-icon>
          </u-form-item>

          <u-form-item label="投诉对象" :borderBottom="false" :required="true">
            <u--input v-model="form.complaintObject" disabledColor="#ffffff" placeholder="请填写" :maxlength="30"
              border="none"></u--input>
          </u-form-item>

          <view style="padding-top: 20rpx"></view>
          <view class="forTitle">
            <view> <text class="forReq">*</text>投诉内容 </view>
          </view>
          <view class="text-AreaView">
            <u--textarea v-model="complaintContent" placeholder="为了帮助我们更好地为群众服务，请用简洁的文字描述您要投诉的内容" count maxlength="300"
              placeholderClass="placeholderClass"></u--textarea>
          </view>

          <view style="padding-top: 20rpx"></view>
          <view class="forTitle">
            <view>图片上传</view>
            <view class="state">（最多可上传3张图片，非必填）</view>
          </view>
          <u-upload :fileList="fileList" @afterRead="afterRead" :capture="['’album']" @delete="deletePic" name="file"
            :maxCount="3" :multiple="true" width="96" height="90">
            <image :src="baseImgUrl + 'upload-icon5.png'" style="width: 200rpx; height: 180rpx"></image>
          </u-upload>
        </u-form>
      </view>
    </view>

    <view class="btns thispageBtn">
      <button class="cancel-btn lt" type="primary" @click="navBack()">
        取消
      </button>
      <button class="submit-btn" type="primary" @click="submitHandle">
        保存
      </button>
    </view>

    <u-picker v-if="pickerShow" :show="pickerShow" :columns="columns" :keyName="keyName" @confirm="showConfirm"
      @close="pickerClose" @cancel="pickerCancel" :closeOnClickOverlay="true"></u-picker>
  </view>
</template>

<script>
  import {
    complaintMessageSave
  } from "@/api/home.js";
  import {
    findEnableDictItemsbyCode
  } from "@/api/api.js";
  import {
    Toast,
    navBack,
    handleClickRouter,
    uniTrim,
    pvwImage,
    useNetWork,
    isLoading,
    getHeader,
    getLocation,
    uploadFile,
  } from "@/utils/util.js";
  import mixin from "@/utils/mixin.js";

  export default {
    name: "newComplaint",
    mixins: [mixin],
    data() {
      return {
        baseImgUrl: this.$baseImgUrl,
        baseURL: this.$baseURL,
        imgPrefix: uni.getStorageSync("imgPrefix"),
        options: {},
        form: {},
        errorType: ["none"],
        complaintContent: "",
        fileList: [],
        complaintImagesUrlList: [],
        fileNum: 0, // 记录长传图片张数
        lnglat: {}
      };
    },
    async onLoad(options) {
      this.options = options;
      uni.showLoading({
        title: '正在获取当前位置信息，请稍等！'
      })
      let location = await getLocation()
      uni.hideLoading()
      let lnglat = {
        lng: location.longitude,
        lat: location.latitude
      }
      this.lnglat = lnglat
    },
    onShow() {
      uni.$on("leadPoliceStation", (v) => {
        let {
          policeAgencyName,
          id
        } = v[0];
        this.$set(this.form, "policeAgencyId", id);
        this.$set(this.form, "policeAgencyName", policeAgencyName);
        this.form = Object.assign({}, this.form);
      });
    },
    onUnload() {
      uni.$off("leadPoliceStation");
    },
    methods: {
      handleClickRouter,
      navBack,
      pvwImage,
      policeAgencyFoces() {
        uni.navigateTo({
          url: `/leadReporting/leadReporting/leadPoliceStation?lng=${this.lnglat.lng}&lat=${this.lnglat.lat}`,
        });
      },
      /* cell 点击 */
      cellClick(name, type) {
        this.keyName = name;
        this.cellType = type;
        let code = "";
        switch (this.cellType) {
          case "complaintClassificationDesc":
            code = "complaintClassificationDesc";
            break;
          default:
            break;
        }

        if (code == "complaintClassificationDesc") {
          findEnableDictItemsbyCode("complaint_classification").then((res) => {
            this.columns[0] = res.result || [];
            this.pickerShow = true;
          });
        }
      },
      /* 选择器 确定 */
      showConfirm(e) {
        let arr = e.value;
        if (arr.length) {
          let obj = arr[0];
          switch (this.cellType) {
            case "complaintClassificationDesc":
              this.form.complaintClassification = obj.value;
              this.form.complaintClassificationDesc = obj.title;
              break;
          }
        }
        this.pickerShow = false;
      },
      afterRead(info) {
        useNetWork(); // 判断是否有网络
        isLoading();

        let _this = this;
        _this.fileNum += info.file.length;
        for (let v of info.file) {
          uploadFile({
              url: "/web/sys/upload/upload_file",
              filePath: v.url,
              name: "file",
              formData: {
                type: 50,
              },
            })
            .then((res) => {
              let {
                fullPath,
                halfPath
              } = res.result;
              _this.fileList.push({
                url: fullPath
              });
              _this.complaintImagesUrlList.push(halfPath);

              if (_this.complaintImagesUrlList.length == _this.fileNum) {
                isLoading(false);
              }
            })
            .finally(() => {
              isLoading(false);
            })
        }
      },
      deletePic(e) {
        this.fileList.splice(e.index, 1);
        this.complaintImagesUrlList.splice(e.index, 1);
        this.fileNum -= 1;
      },
      submitHandle() {
        let {
          policeAgencyId,
          policeAgencyName,
          complaintClassification,
          complaintClassificationDesc,
          complaintObject,
        } = this.form;

        if (!policeAgencyName) {
          Toast("请选择派出所");
          return;
        }

        if (!complaintClassificationDesc) {
          Toast("请选择投诉分类");
          return;
        }

        if (!complaintObject) {
          Toast("请选择投诉对象");
          return;
        }

        if (!this.complaintContent) {
          Toast("请输入投诉内容");
          return;
        }

        let data = {
          policeAgencyId,
          policeAgencyName,
          complaintClassification,
          complaintObject,
          complaintContent: this.complaintContent,
          complaintImagesUrlList: this.complaintImagesUrlList,
        };

        complaintMessageSave(data).then((res) => {
          if (res.code == 200) {
            Toast("保存成功");
            uni.setStorageSync("comAndadvice", true);
            navBack();
            return;
          }
          Toast(res.message);
        });
      },
    },
  };
</script>

<style>
  page {
    background-color: #f3f3f3 !important;
    height: 100%;
  }
</style>
<style lang="scss" scoped>
  .newComplaint {
    flex: 1;
    background-color: #f3f3f3;

    .newComplaint-title {
      position: absolute;
      top: 10rpx;
      left: 6%;
      color: #ffffff;
      font-size: 34rpx;
      font-weight: bold;
    }

    .wrapper {
      position: absolute;
      top: 30rpx;
      left: 50%;
      transform: translateX(-50%);

      .wrapper-item {
        margin-bottom: 2vh;
        padding: 35rpx 30rpx;
        background-color: #ffffff;
        border-radius: 10rpx;
        width: 710rpx;
      }

      .content {
        margin: 0 auto;
        height: calc(100vh - 90px);
        overflow-y: auto;
      }

      /deep/.u-form-item__body__right__content {
        padding: 12rpx 18rpx;
        border-radius: 8rrpx;
        background: #f9f9f9;
      }

      /deep/.u-input__content {
        background: #f9f9f9;
      }

      .seleTime {
        width: 30rpx;
        height: 30rpx;
      }

      .forTitle {
        display: flex;
        position: relative;
        margin-bottom: 15rpx;
        font-size: 30rpx;

        .forReq {
          position: absolute;
          left: -9px;
          color: #f56c6c;
          line-height: 20px;
          font-size: 20px;
          top: 3px;
        }

        .state {
          color: #a1a1a1;
          font-size: 26rpx;
        }
      }

      .text-AreaView {

        /deep/.u-textarea,
        .u-textarea__count {
          background-color: #f9f9f9 !important;
        }

        .u-textarea__field {
          font-weight: 400;
        }

        /deep/ .input-placeholder {
          font-weight: 400;
        }
      }
    }

    .thispageBtn {
      position: absolute;
      width: 100vw;
      bottom: 0;
    }
  }
</style>